<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Qcate;
use App\Admin\Models\Questions;
use Common\ControllerAdBase;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class QuestionsController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }


    /**
     * 获取分类
     */
    public function qcateAction(){
        $qcate = new Qcate();
        $catelist = $qcate->catelist();
        $this->ok($catelist);
    }


    /**
     * 问题列表
     */
    public function listAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }
        //当前条数
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $pageSize = 10;
        }else{
            $pageSize = (int)$this->params['limit'];
        }
        $question = new Questions();
        $catelist = $question->question_list($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $catelist,
                "limit"=> $pageSize,
                "page" => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $list_items = ToolsController::paging($list_items);
        foreach($list_items['items'] as $key=>$value){
            $qcate = new Qcate();
            $qcate_arr = $qcate->find_first_by_Id($value['qcat_id']);
            $list_items['items'][$key]['qcate_name'] = $qcate_arr ? $qcate_arr['name'] : '分类已删除';
        }
        $this->ok($list_items);
    }

    /**
     * 更新问题
     */
    public function updatequestAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'问题id不能为空');
        }
        $this->validate_field($this->params);
        $question = new Questions();
        $find_first = $question->find_first_by_Id($this->params['id']);
        $find_first['qcat_id']=$this->params['cateid'];
        $find_first['question']=$this->params['question'];
        $find_first['answer']=$this->params['answer'];
        $find_first['sort']=$this->params['sort'];
        $find_first['isshow']=$this->params['isshow'];
        $find_first['updated_at'] = date('Y-m-d H:i:s',time());
        $result = $question->save_question($find_first);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }
    }

    /**
     * 添加问题
     */
    public function addquestAction(){
        $this->validate_field($this->params);
        $question = new Questions();
        $find_first['qcat_id']=$this->params['cateid'];
        $find_first['question']=$this->params['question'];
        $find_first['answer']=$this->params['answer'];
        $find_first['sort']=$this->params['sort'];
        $find_first['isshow']=$this->params['isshow'];
        $find_first['created_at'] = date('Y-m-d H:i:s',time());
        $find_first['updated_at'] = date('Y-m-d H:i:s',time());
        $result = $question->save_question($find_first);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'添加失败');
        }
    }


    private function validate_field($params){
        if(empty($params['cateid'])){
            $this->fail(-1,'问题分类必须选择');
        }
        if(empty($params['question'])){
            $this->fail(-1,'问题标题必须填写');
        }
        if(empty($params['answer'])){
            $this->fail(-1,'问题答案必须填写');
        }
    }

    /**
     * 删除问题
     */
    public function delquestAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'问题id不能为空');
        }
        $question = new Questions();
        $result = $question->findFirstById($this->params['id'])->delete();
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'删除失败');
        }
    }


    /**
     * 分类列表
     */
    public function catelistquestAction(){
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }
        //当前条数
        if(!isset($this->params['limit']) || empty($this->params['limit'])){
            $pageSize = 10;
        }else{
            $pageSize = (int)$this->params['limit'];
        }
        $qcate = new Qcate();
        $catelist = $qcate->catelist($this->params);
        $paginator = new PaginatorModel(
            array(
                "data" => $catelist,
                "limit"=> $pageSize,
                "page" => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $this->ok($list_items);
    }


    /**
     * 修改问题分类
     */
    public function editcatequestAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'分类id不能为空');
        }
        $this->validate_cate($this->params);
        $qcate = new Qcate();
        $qcate_find = $qcate->find_first_by_Id($this->params['id']);
        $qcate_find['name'] = $this->params['name'];
        $qcate_find['icon'] = $this->params['icon'];
        $qcate_find['sort'] = $this->params['sort'];
        $qcate_find['updated_at'] = date('Y-m-d H:i:s',time());
        $result = $qcate->qcate_save($qcate_find);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }
    }

    /**
     * 添加分类
     * @param $params
     */
    public function addcatequestAction(){
        $this->validate_cate($this->params);
        $qcate = new Qcate();
        $qcate_find['name'] = $this->params['name'];
        $qcate_find['icon'] = $this->params['icon'];
        $qcate_find['sort'] = $this->params['sort'];
        $qcate_find['created_at'] = date('Y-m-d H:i:s',time());
        $qcate_find['updated_at'] = date('Y-m-d H:i:s',time());
        $result = $qcate->qcate_save($qcate_find);
        if($result){
            $this->ok([]);
        }else{
            $this->fail(-1,'修改失败');
        }
    }



    private function validate_cate($params){
        if(empty($params['name'])){
            $this->fail(-1,'分类名称不能为空');
        }
        if(empty($params['icon'])){
            $this->fail(-1,'分类图标不能为空');
        }
    }


    /**
     * 删除分类
     */
    public function delcatequestAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'分类id不能为空');
        }
        $question = new Questions();
        $count_number = $question->mycount($this->params['id']);
        if($count_number){
            $this->fail(-1,'该分类下面有问题列表，不能删除');
        }else{
            $cate = new Qcate();
            $result = $cate->findFirstById($this->params['id'])->delete();
            if($result){
                $this->ok([]);
            }else{
                $this->fail(-1,'删除失败');
            }
        }
    }

}
